<template>
  <el-container class="rootbox">
    <el-main style="    padding: 0px;">
      <el-form class="form-query" ref="form" label-width="200px"  @submit.native.prevent :inline="true" style="    padding-left: 45px;" size="larger" >
        <el-form-item label="普通标签打印机" required>
          <el-select ref="printerType1"  v-model="printerType1" filterable clearable placeholder="请选择"   style="    width: 220px;" @change="printerTypeChange('printerType1','rfidprinter_type1')">
            <el-option value>请选择</el-option>
            <el-option
              v-for="item in printerList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="抗金属标签打印机" required>
          <el-select ref="printerType2" v-model="printerType2" filterable clearable placeholder="请选择"   style="    width: 220px;" @change="printerTypeChange('printerType2','rfidprinter_type2')">
            <el-option value>请选择</el-option>
            <el-option
              v-for="item in printerList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label-width="0px">
          <el-button type="primary" @click="addPrint">添加打印机</el-button>
        </el-form-item>

        <el-form-item label="说明" >
          <h5 style="display: inline; padding-left: 11px;">目前支持rfid打印，数据为外部接口调用产生。</h5>
        </el-form-item>
      </el-form>
      <el-row >
        <el-col :span="24">
          <data-view
            ref="dataview"
            propViewKey="wm_order_barcodeout"
            :propAutoFetchData="false"
            :propSubHeight="120"
            class="data-view-bar"
            @extra-button-click="handleExtraButton"
            @after-load="afterLoad"
          />
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>
<script>
/*eslint-disable*/

export default {
  remark: '标签打印外部，即通过 spdAms.printRFID接口 创建',
  data() {
    return {
      printer_label_server_name: '未设置',
      printerType1: '',
      printerType2: '',
      printerList: []
    }
  },
  methods: {
    //搜索表格
    gridSearch(isReset) {
      let wrapper = this.$newWrapper()
      this.$refs.dataview.setQueryWrapper(wrapper)
    },
    //获取打印服务器
    getPrint() {
      let wrapper = this.$newWrapper()
      wrapper.eq('type_key','rfid')
      const params = {
        queryString: this.utilVue.json2str(wrapper.queryConditions),
        orderString: null
      }
      this.$ajax.get('/sys/printer/list', { params },{
        params
      }).then(resp => {
        this.printerList = resp.data
        //当前所设置的打印机
        this.printerType1 = this.utils.loadUserState(this,'rfidprinter_type1')
        this.printerType2 = this.utils.loadUserState(this,'rfidprinter_type2')
      })
    },
    //选择打印服务器
    printerTypeChange(key, storageKey) {
      this.utils.updateUserState(storageKey, this[key], this)
    },
    //添加打印机
    addPrint() {
      global.utilPrint.openDialog('dataview','sys_printer',null, '添加打印机', 'sys/printer/detail.vue', ()=> {
        this.getPrint()
      })
    },
    //按钮事件
    async handleExtraButton(para) {
      //行打印或者表头打印
      if (para.button.key == 'printmore' || para.button.key == 'printone') {
        let rows=[];
        if(para.button.key == 'printmore'){ //多选
          rows = this.$refs.dataview.$refs.tbMain.getCheckboxRecords()
        }else{
          rows.push(para.data)
        }
        if (!rows || rows.length <1) {
          this.utilMessage.warning('请选择需要打印的记录')
          return
        }

        //按照标签类型分别打印
        let rows1 = rows.filter(row => row.labelType == 'type1')
        let rows2 = rows.filter(row => row.labelType == 'type2')

        if (rows1 && rows1.length > 0 && this.strings.isBlank(this.printerType1)) {
          this.utilMessage.warning('请先选择普通标签打印机')
          this.$refs.printerType1.focus()
          return
        }

        if (rows2 && rows2.length > 0 && this.strings.isBlank(this.printerType2)) {
          this.utilMessage.warning('请先选择抗金属标签打印机')
          this.$refs.printerType2.focus()
          return
        }

        await this.doPrint(rows1, this.printerType1, '开始打印普通标签')
        await this.doPrint(rows2, this.printerType2, '开始打印抗金属标签')

      }
    },

    //进行打印
    async doPrint(rows, printServerId, msg) {
      if (!rows || rows.length < 1) return
      this.utilMessage.info(msg)
      this.utilVue.loadShow(true)
      let params={
        rfidArr:[],
        printerId: printServerId
      }
      rows.forEach(ob => {params.rfidArr.push(ob.rfid)})
      await this.$ajax.post('/spdAms/printRFIDOut',params, {timeout: 1000 * 60 * 10}).then(resp => {
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
          this.$refs.dataview.ocRefresh()
        }
        this.utilVue.loadHide()
      })
    },

    afterLoad(){
    }
  },
  props: {
  },
  mounted() {
    this.gridSearch()
    this.getPrint()
  }
}
</script>

<style lang="scss" scoped>
  .rootbox{
    height: 100%;
    padding: 5px;
  }

  .form-query .el-form-item{
    margin-bottom: 0px;
  }
</style>
